package java.Dao.Impl;

import Dao.KnowledgeBaseMemberDAO;
import entity.KnowledgeBaseMember;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Su
 * @Date 2025/4/29 21:51
 * @Version 1.0 （版本号）
 */
public class KnowledgeBaseMemberDAOImpl implements KnowledgeBaseMemberDAO {
    private final Connection connection;

    public KnowledgeBaseMemberDAOImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    public boolean addMember(int knowledgeBaseId, int userId, String role) throws SQLException {
        String sql = "INSERT INTO knowledge_base_member (knowledge_base_id, user_id, role) VALUES (?, ?, ?)";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, knowledgeBaseId);
            stmt.setInt(2, userId);
            stmt.setString(3, role);
            return stmt.executeUpdate() > 0;
        }
    }

    @Override
    public boolean removeMember(int knowledgeBaseId, int userId) throws SQLException {
        String sql = "DELETE FROM knowledge_base_member WHERE knowledge_base_id = ? AND user_id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, knowledgeBaseId);
            stmt.setInt(2, userId);
            return stmt.executeUpdate() > 0;
        }
    }

    @Override
    public boolean updateMemberRole(int knowledgeBaseId, int userId, String role) throws SQLException {
        String sql = "UPDATE knowledge_base_member SET role = ? WHERE knowledge_base_id = ? AND user_id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setString(1, role);
            stmt.setInt(2, knowledgeBaseId);
            stmt.setInt(3, userId);
            return stmt.executeUpdate() > 0;
        }
    }

    @Override
    public boolean isAdmin(int knowledgeBaseId, int userId) throws SQLException {
        String sql = "SELECT 1 FROM knowledge_base_member WHERE knowledge_base_id = ? AND user_id = ? AND role = 'ADMIN'";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, knowledgeBaseId);
            stmt.setInt(2, userId);
            ResultSet rs = stmt.executeQuery();
            return rs.next();
        }
    }

    @Override
    public boolean isMember(int knowledgeBaseId, int userId) throws SQLException {
        String sql = "SELECT 1 FROM knowledge_base_member WHERE knowledge_base_id = ? AND user_id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, knowledgeBaseId);
            stmt.setInt(2, userId);
            ResultSet rs = stmt.executeQuery();
            return rs.next();
        }
    }

    @Override
    public List<KnowledgeBaseMember> listMembers(int knowledgeBaseId) throws SQLException {
        List<KnowledgeBaseMember> result = new ArrayList<>();
        String sql = "SELECT kbm.id, kbm.knowledge_base_id, kbm.user_id, kbm.role, kbm.joined_at, u.username " +
                "FROM knowledge_base_member kbm JOIN user u ON kbm.user_id = u.user_id " +
                "WHERE kbm.knowledge_base_id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, knowledgeBaseId);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                result.add(new KnowledgeBaseMember(
                        rs.getInt("id"),
                        rs.getInt("knowledge_base_id"),
                        rs.getInt("user_id"),
                        rs.getString("username"),
                        rs.getString("role"),
                        rs.getDate("joined_at")
                ));
            }
        }
        return result;
    }
}
